package com.qili.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.base.controller.BaseController;
import com.qili.core.utils.DictTypes;
import com.qili.entity.base.BaseDict;
import com.qili.entity.exam.ExExamExportModel;
import com.qili.entity.exam.ExUserExportModel;
import com.qili.entity.exam.dto.ExStuExamCopyDTO;
import com.qili.service.base.BaseDictService;
import com.qili.service.exam.ExExamService;
import com.qili.service.exam.ExamStatisticsService;
import com.qili.util.ReType;
import com.qili.util.StringUtil;
import com.spire.doc.interfaces.IField;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>统计模块</p>
 *
 * @author zengyuli
 * @date 2021/9/1
 * @description
 */
@Controller
@Slf4j
@RequestMapping("/ex/statistics")
public class ExamStatisticsCountController extends BaseController {
    @Autowired
    ExExamService exExamService;
    @Autowired
    BaseDictService baseDictService;
    @Autowired
    ExamStatisticsService examStatisticsService;

    /**
     * 答卷统计页面
     *
     * @return
     */
    @GetMapping(value = "/answerCountIndex")
    @RequiresPermissions("zh:djtj")
    public String answerCountList(Model model) {
        //考试分类
        List<BaseDict> ksfl = baseDictService.selectByTypeCode(DictTypes.KSFL.getType());
        model.addAttribute("ksfl", ksfl);
        //试卷分类
        List<BaseDict> sjfl = baseDictService.selectByTypeCode(DictTypes.SJFL.getType());
        model.addAttribute("sjfl", sjfl);
        return "synthesize/sjk/answerCount";
    }

    /**
     * 获取答卷统计列表
     *
     * @return
     */
    @GetMapping("getAnswerCountList")
    @ResponseBody
    public ReType getAnswerCountList(ExStuExamCopyDTO exStuExamCopyDTO, int page, int limit) {
        Page<ExExamExportModel> pageHelper = PageHelper.startPage(page, limit);
        examStatisticsService.getAnswerCountList(exStuExamCopyDTO);
        return new ReType(pageHelper.getTotal(), pageHelper.getResult());
    }


    /**
     * 导出答卷统计
     * @param exStuExamCopyDTO
     * @param response
     */
    @GetMapping("exportAnswerCountList")
    public void exportAnswerCountList(ExStuExamCopyDTO exStuExamCopyDTO, HttpServletResponse response) {
        String exportTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        List<ExExamExportModel> result = examStatisticsService.getAnswerCountList(exStuExamCopyDTO);
        for (ExExamExportModel exExamExportModel : result) {
            if (StringUtil.isNotBlank(exExamExportModel.getIsJg())){
                if ("1".equals(exExamExportModel.getIsJg())){
                    exExamExportModel.setIsJg("是");
                }else if ("0".equals(exExamExportModel.getIsJg())){
                    exExamExportModel.setIsJg("否");
                }
            }
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("答卷统计","统计"), ExExamExportModel.class, result);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("答卷统计"+exportTime+".xls", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转成员统计页面
     *
     * @param model
     * @return
     */
    @GetMapping(value = "/memberCount")
    @RequiresPermissions("zh:cytj")
    public String memberCountList(Model model) {
        List<BaseDict> kslx = baseDictService.selectByTypeCode(DictTypes.KSFL.getType());
        model.addAttribute("kslx", kslx);
        return "synthesize/sjk/memberCount";
    }


    /**
     * 获取成员统计列表
     *
     * @return
     */
    @GetMapping("getUserExamCountList")
    @ResponseBody
    public ReType getUserExamCountList(ExStuExamCopyDTO exStuExamCopyDTO, int page, int limit) {
        Page<ExExamExportModel> pageHelper = PageHelper.startPage(page, limit);
        examStatisticsService.getUserExamCountList(exStuExamCopyDTO);
        return new ReType(pageHelper.getTotal(), pageHelper.getResult());
    }


    /**
     * 导出成员统计
     * @param exStuExamCopyDTO
     * @param response
     */
    @GetMapping("exportUserExamCountList")
    public void exportUserExamCountList(ExStuExamCopyDTO exStuExamCopyDTO, HttpServletResponse response) {
        String exportTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        List<ExUserExportModel> result = examStatisticsService.getUserExamCountList(exStuExamCopyDTO);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("成员统计","统计"), ExUserExportModel.class, result);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("成员统计"+exportTime+".xls", "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
